package com.kehutong.work.order.manage.mini;

import javax.servlet.http.HttpServletRequest;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;
import com.kehutong.common.TokenService;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import com.kehutong.work.order.manage.entity.WorkOrderManage;
import com.kehutong.work.order.manage.service.WorkOrderClassifyService;
import com.kehutong.work.order.manage.service.WorkOrderManageService;
import com.kehutong.work.order.manage.service.WorkOrderTypeService;

/**
 * 小程序端-工单
 */
@MvcAction
@ReqMapping("/kht-bin/mini/work/order/manage")
public class WorkOrderAction {

    @Inject
    private WorkOrderManageService manageService;
    @Inject
    private WorkOrderTypeService typeService;
    @Inject
    private WorkOrderClassifyService classifyService;
    @Inject
    private TokenService tokenService;

    @ReqMapping("/list/page")
    public Object listPage(HttpServletRequest req, JSONObject jsonObject) throws Exception {
        Token token = tokenService.getUserToken(req);
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return Result.success(manageService.listPage(jsonObject, token.getUuid(), true));
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(HttpServletRequest req, WorkOrderManage manage) throws Exception {
        Token token = tokenService.getUserToken(req);
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        token.setCompanyNo(req.getHeader("companyNo"));
        return manageService.save(manage, token, true);
    }

    @ReqMapping("/type/list")
    public Object typeList(JSONObject jsonObject) {
        return typeService.list(jsonObject);
    }

    @ReqMapping("/classify/list")
    public Object classifyList(JSONObject jsonObject) {
        return classifyService.list(jsonObject);
    }

    @ReqMapping("/handle/order")
    public Object handleOrder(HttpServletRequest req, JSONObject jsonObject) throws Exception {
        Token token = tokenService.getUserToken(req);
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        String userName = jsonObject.getString("createUserName");
        if (Objects.isEmpty(userName)) {
            userName = token.getUsername();
        }
        jsonObject.put("companyNo", req.getHeader("companyNo"));
        return manageService.handleWorkOrder(token.getUuid(), userName, jsonObject, true);
    }

    @ReqMapping("/detail")
    public Object detail(HttpServletRequest req, String id) throws Exception {
        Token token = tokenService.getUserToken(req);
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return manageService.detail(token, id, false);
    }

    @ReqMapping("/delete")
    public Object delete(HttpServletRequest req, WorkOrderManage manage) throws Exception {
        Token token = tokenService.getUserToken(req);
        if (!checkLogin(token)) {
            return Result.error(0, "请先登录");
        }
        return manageService.delete(manage, token.getUuid());
    }

    @ReqMapping("/reminders")
    public Object reminders(String id) {
        return manageService.reminders(id);
    }

    private boolean checkLogin(Token token) {
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }
}
